# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : orderlines_paused_view.py
# Time       ：2023/8/18 22:12
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    暂停流程
"""
import traceback

from flask import request
from flask_restx import fields

from apis.orderlines import orderlines_ns
from public.api_utils.api_decorator import handle_api_error
from public.base_response import generate_response, MarshalModel
from public.base_view import AbstractView
from public.logger import logger

paused = {
    'process_instance_id': fields.String(description='流程实例ID'),
    'stop_schedule': fields.Boolean(description='是否停止定时')
}
orderlines_paused_marshal = MarshalModel('paused', orderlines_ns)


class OrderlinesPausedView(AbstractView):
    url = '/paused'

    def __init__(self, *args, **kwargs):
        super(OrderlinesPausedView, self).__init__(*args, **kwargs)
        self.form_data = request.json
        self.is_logger = True

    @handle_api_error()
    @orderlines_ns.expect(orderlines_paused_marshal.detail_request(paused))
    @orderlines_ns.marshal_with(orderlines_paused_marshal.create_response(True))
    def post(self):
        """流程暂停"""
        try:
            from orderlines import OrderLines
            process_instance_id = self.form_data.get('process_instance_id')
            stop_schedule = self.form_data.get('stop_schedule')
            OrderLines().paused_process(process_instance_id, stop_schedule)
            return generate_response(message='process begin paused')
        except Exception as e:
            logger.error(f'流程暂停失败:{e}\n {traceback.format_exc()}')
            return generate_response(code=500, message='process paused failure')
